using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using XFGameFramework;
using XFGameFramework.BuffSystem;

public class EnemyController : Controller
{
    private EnemyModel enemymodel;
    public Action OnEnemydead;
    public Action OnStrengthDepleted;
    public Action OnNeatnessDepleted;
    public Action OnAgilityDepleted;
    public override void OnInit()
    {

        base.OnInit();
        enemymodel = gameSaveController.GetEnemyModel(11);

    }
    private GameSaveController gameSaveController => Module.LoadController<GameSaveController>();
    private GameController gameController => Module.LoadController<GameController>();
    public void CreatEnemy(int id, EnemyConfigData enemyConfigData)
    {
        gameSaveController.CreateEnemyModel(11);
        Debug.Log("创建敌人，id："+id+"，名称：" + enemyConfigData.name);
        enemymodel = gameSaveController.GetEnemyModel(11);
        enemymodel.id = id;
        enemymodel.name = enemyConfigData.name;
        enemymodel.level = enemyConfigData.level;
        enemymodel.maxHp = enemyConfigData.maxHp;

        enemymodel.strength = enemyConfigData.strength;

        enemymodel.Agility = enemyConfigData.Agility;
        enemymodel.currentHp = enemymodel.maxHp;
        enemymodel.currentStrength = enemymodel.strength;
        enemymodel.CurrentAgility = enemymodel.Agility;
        
        enemymodel.skillsID.Add(10002);
        enemymodel.skillsCD.Add(2);

        LogUtil.Log("敌人技能CD:" + enemymodel.skillsCD[0]);

        enemymodel.Buffs = new List<BuffBase>();

    }

    public EnemyModel GetEnemyModel()
    {
        return enemymodel;
    }
    public void Set(EnemyModel enemyModel, EnemyConfigData configData)
    {
        enemymodel.level = configData.level;
        enemymodel.maxHp = configData.maxHp;
        enemymodel.strength = configData.strength;
        enemymodel.physique = configData.physique;
        enemyModel.Agility = configData.Agility;
        enemymodel.currentHp = enemymodel.maxHp;
        enemymodel.currentStrength = enemymodel.strength;
    }
    /// <summary>
    /// 判断玩家是否死亡
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public bool IsDead(EnemyModel model)
    {
        if (model.currentHp <= 0)
        {
            model.currentHp = 0;
            return true;
        }

        return false;
    }

    public void EnemySkillsCDUp()
    {
        for (int i = 0 ; i <= enemymodel.skillsCD.Count - 1; i++)
        {
            Debug.Log("敌人技能CD:" + enemymodel.skillsCD[i]);
            enemymodel.skillsCD[i] = Mathf.Max(0, enemymodel.skillsCD[i] - 1);

        }
    }
    /// <summary>
    /// 判断技能是否可以使用
    /// </summary>
    /// <param name="skillId"></param>
    public bool CanUseSkill(int skillId)
    {
        int skillIndex = 0;
        for (int i = 0; i <= enemymodel.skillsID.Count - 1; i++)
        {
            if (enemymodel.skillsID[i] == skillId)
            {
                skillIndex = i;
                break;
            }
        }
        Debug.Log("敌人技能索引:" + skillIndex );
        if (enemymodel.skillsCD[skillIndex] == 0)
        {
            return true;
        }
        else return false;
    }
    public List<int> GetSkillList()
    {
        return enemymodel.skillsID;
    }
    
    #region 属性
    public int CurrentHp
    {
        get => enemymodel.currentHp;
        set
        {
            enemymodel.currentHp = Mathf.Max(0, value);//玩家生命为非负数
            enemymodel.currentHp = Mathf.Min(value, enemymodel.maxHp);//玩家生命不超过最大生命值
            if (enemymodel.currentHp == 0)
            {   
                gameController.SetGameState(GameState.Fight_Win);
                OnEnemydead?.Invoke();
            }
        }
    }
    public int CurrentStrength
    {
        get => enemymodel.currentStrength;
        set
        {
            enemymodel.currentStrength = Mathf.Max(0, value);//玩家力量为非负数
            enemymodel.currentStrength = Mathf.Min(value, enemymodel.strength);//玩家力量不超过最大力量值
            if (enemymodel.currentStrength <= 0)
            {
                OnStrengthDepleted?.Invoke();
            }
        }
    }
    public int CurrentAgility
    {
        get => enemymodel.CurrentAgility;
        set 
        {
            enemymodel.CurrentAgility = Mathf.Max(0, value);//玩家敏捷为非负数
            enemymodel.CurrentAgility = Mathf.Min(value, enemymodel.Agility);//玩家敏捷不超过最大敏捷值
            if (enemymodel.CurrentAgility <= 0)
            {
                OnAgilityDepleted?.Invoke();
            }
        }
    }

    public int fullHp
    {
        get => enemymodel.maxHp;
    }
    public int fullStrength
    {
        get => enemymodel.strength;
    }
    public int level
    {
        get => enemymodel.level;
    }
    public string name
    {
        get => enemymodel.name;
    }
    #endregion

}
